/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETSI_LogicAc.cc
  \date			september 2011
  \author		TNick

  \brief		Contains the implementation for ETSI_LogicAc class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QFontMetrics>
#include	<QPainter>

#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>
#include	<sheet/subitems/ETSI_Root.h>
#include	<sheet/subitems/ETSI_SText.h>

#include	"ETSI_LogicAc.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */
DDHC_CONTENT(ETSI_LogicAc)

/* ------------------------------------------------------------------------- */
ETSI_LogicAc::ETSI_LogicAc	( void )
	: ETSI_Rows()
{

	DDHC_CONSTRUCT;
	DDHC_SUBITEM_VARS;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_LogicAc::~ETSI_LogicAc	( void )
{
	DDHC_DESTRUCT
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_LogicAc::hitTest			(HtData * h_data)
{

	if (h_data->ck_pt.x() < h_data->ref_pt.x())
	{
		h_data->it_hitted = this;
		h_data->true_hit = false;
		/* h_data->rct_bbx already set by the caller */
		h_data->int_data = 0;
		return;
	}

	h_data->rct_bbx.setLeft( h_data->rct_bbx.left() + signWidth() );

	ETSI_Rows::hitTest(h_data);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_LogicAc::paint		(
		ETSheetView * vp, QPainter * painter, QPointF pt_ref )
{

	PaintData	pd(vp, painter, pt_ref);
	prePaint(&pd);

	QPen pn_t = painter->pen();
	QPen new_p;
	new_p.setColor(pn_t.color());
	new_p.setCosmetic(false);
	new_p.setCapStyle(Qt::RoundCap);
	new_p.setStyle(Qt::SolidLine);
	new_p.setJoinStyle(Qt::RoundJoin);
	new_p.setWidth(1);

	painter->setPen(new_p);

	if (isMarkerLine())
	{
		qreal	x_base2 = pt_ref.x() - signWidth()*0.5;
		painter->drawLine(
					x_base2, pt_ref.y() + bbox.top(),
					x_base2, pt_ref.y() + bbox.bottom()
					);

	}
	else if (isMarkerRound())
	{
		/* as the reference point is guaranteed to be at half-height, the
	h_half works; it would not work otherwise */
		qreal h_half = bbox.height() / 2;
		QPointF bs_p1[] = {
			QPointF(pt_ref.x(),							pt_ref.y() - h_half),
			QPointF(pt_ref.x() - signWidth()*0.5,		pt_ref.y() - h_half*0.9),
			QPointF(pt_ref.x() - signWidth()*0.9,		pt_ref.y() - h_half*0.7),
			QPointF(pt_ref.x() - signWidth(),			pt_ref.y()),
			QPointF(pt_ref.x() - signWidth()*0.9,		pt_ref.y() + h_half*0.7),
			QPointF(pt_ref.x() - signWidth()*0.5,		pt_ref.y() + h_half*0.9),
			QPointF(pt_ref.x(),							pt_ref.y() + h_half)
		};
		painter->drawPolyline(&bs_p1[0], 7);
	}
	else if (isMarkerAngle())
	{
		/* as the reference point is guaranteed to be at half-height, the
	 pt_ref.y() works; it would not work otherwise */
		QPointF bs_p1[] = {
			QPointF(pt_ref.x(),							pt_ref.y() + bbox.y()),
			QPointF(pt_ref.x() - signWidth(),			pt_ref.y()),
			QPointF(pt_ref.x(),							pt_ref.y() + bbox.bottom())
		};
		painter->drawPolyline(&bs_p1[0], 3);

	}
	else if (isMarkerSquare())
	{
		QPointF bs_p1[] = {
			QPointF(pt_ref.x(),							pt_ref.y() + bbox.y()),
			QPointF(pt_ref.x() - signWidth(),			pt_ref.y() + bbox.y()),
			QPointF(pt_ref.x() - signWidth(),			pt_ref.y() + bbox.bottom()),
			QPointF(pt_ref.x(),							pt_ref.y() + bbox.bottom())
		};
		painter->drawPolyline(&bs_p1[0], 4);

	}
	else // if (isMarkerDefault())
	{
		QPointF bs_p[] = {
			QPointF(pt_ref.x(),								pt_ref.y() + bbox.y()),
			QPointF(pt_ref.x() - signWidth() / 4,			pt_ref.y() + bbox.y() + signWidth() / 8),
			QPointF(pt_ref.x() - signWidth() / 2,			pt_ref.y() + bbox.y() + signWidth() / 2),
			QPointF(pt_ref.x() - signWidth() / 2,			pt_ref.y() - signWidth() / 2),
			QPointF(pt_ref.x() - signWidth() * (0.75),		pt_ref.y() - signWidth() / 8),
			QPointF(pt_ref.x() - signWidth(),				pt_ref.y()),
			QPointF(pt_ref.x() - signWidth() * (0.75),		pt_ref.y() + signWidth() / 8),
			QPointF(pt_ref.x() - signWidth() / 2,			pt_ref.y() + signWidth() / 2),
			QPointF(pt_ref.x() - signWidth() / 2,			pt_ref.y() + bbox.bottom() - signWidth() / 2),
			QPointF(pt_ref.x() - signWidth() / 4,			pt_ref.y() + bbox.bottom() - signWidth() / 8),
			QPointF(pt_ref.x(),								pt_ref.y() + bbox.bottom())
		};
		painter->drawPolyline(&bs_p[0], 11);

	}

	painter->setPen(pn_t);



	ETSI_Rows::paint(vp, painter, pt_ref);

	postPaint(&pd);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_LogicAc::recompute	(bool b_full)
{

	ETSI_Rows::recompute(b_full);
	bbox.setLeft( -signWidth() );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_LogicAc *		ETSI_LogicAc::loadXML		(
		QDomElement dom_e)
{

	ETSI_LogicAc *		ret_it;

#ifdef	APP_DEBUG
	/* type is assumed to be sum */

	QDomElement dom_it = dom_e.firstChildElement(XML_TAG_TYPE);
	if (!dom_it.isNull())
	{
		Q_ASSERT_X(dom_it.text() == XML_SIT_LOGICAC, __FUNCTION__,
				   QString("loadXML got an element of type <"
						   "%1> while expected type is " XML_SIT_LOGICAC)
				   .arg(dom_it.text()).toLatin1()
				   );
	}
#endif

	/* create this element */
	ret_it = new ETSI_LogicAc();

	ret_it->loadXMLBasic(dom_e);

	return ret_it;

}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void				ETSI_LogicAc::saveXML		(
		QDomElement dom_e)
{

	/* save the type */
	QDomElement dom_it = dom_e.ownerDocument().createElement(XML_TAG_TYPE);
	dom_e.appendChild(dom_it);
	QDomText dom_tx = dom_e.ownerDocument().createTextNode(XML_SIT_LOGICAC);
	dom_it.appendChild(dom_tx);

	saveXMLBasic(dom_e);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_LogicAc::kidNavReq_up		(ETSheetView * vp, ETSubItem * kid)
{
	//	if (kid == &top_block)
	//		{
	//			if (fatherSubI() != NULL)
	//				{
	//					fatherSubI()->kidNavReq_up(vp, this);
	//					return true;
	//				}
	//		}
	//	else if (kid == &btm_block)
	//		{
	//			vp->setCurrentSubI(NULL, &top_block);
	//			return true;
	//		}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_LogicAc::kidNavReq_down		(ETSheetView * vp, ETSubItem * kid)
{
	//	if (kid == &top_block)
	//		{
	//			vp->setCurrentSubI(NULL, &btm_block);
	//			return true;
	//		}
	//	else if (kid == &btm_block)
	//		{
	//			if (fatherSubI() != NULL)
	//				{
	//					fatherSubI()->kidNavReq_down(vp, this);
	//					return true;
	//				}
	//		}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
